package com.example.luobaomessage;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity2 extends AppCompatActivity {



    private TextView ftext1;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_1);



        ftext1 = findViewById(R.id.ftext1);
        imageView = findViewById(R.id.imageView);


        getCarmeesage(MyApp.token);









    }



    private void getCarmeesage(String token) {
        String url = "http://124.93.196.45:10001//dev-api/bs-car/car/list";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).addHeader("Authorization", token).get().build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {

                            JSONObject object = new JSONObject(s);
                            JSONArray rows=object.getJSONArray("rows");

                            JSONObject xx=rows.getJSONObject(0);


                          JSONArray imagePaths=xx.getJSONArray("imagePaths");

                          String yy=imagePaths.getString(0);

                            Toast.makeText(MainActivity2.this, "获取车辆信息成功。。。。。。。。。。。。。。。", Toast.LENGTH_SHORT).show();


                            ftext1.setText("当前行驶公里数:"+6+"\n"+"\n"+"车辆距离下次保养剩余公里数:"+7);



                            Glide.with(MainActivity2.this).load("http://124.93.196.45:10001/dev-api/"+yy).into(imageView);



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
              }  );
            }
        });

    }
}